//
//  DCAuthBasicInfoController.m
//  Demo
//
//  Created by zhaolin on 2023/1/9.
//

#import "DCAuthBasicInfoController.h"
#import "DCAuthInfoCommonCell.h"
#import "DCAuthInfoUIModel.h"
#import "DCAuthGenderCell.h"
#import "DCUnknownBVNCell.h"
#import "DCAuthFooterView.h"
#import "DCDatePickerView.h"

@interface DCAuthBasicInfoController ()<UITableViewDelegate,UITableViewDataSource,DCAuthInfoCommonCellDelegate,DCAuthFooterViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) DCDatePickerView *pickView;
@end

@implementation DCAuthBasicInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitleName:@"Basic Information"];

    // Do any additional setup after loading the view.
}

//- (DCDatePickerView *)pickView {
//    if(!_pickView) {
//        mWeakSelf;
//        _pickView = [[DCDatePickerView alloc] initWithFrame:self.view.bounds];
//        _pickView.cancelCallBack = ^(NSString * _Nonnull date) {
//            [weakSelf pickViewShow:NO];
//        };
//        _pickView.sureCallBack = ^(NSString * _Nonnull date) {
//            for ( DCAuthInfoUIModel *model in weakSelf.dataSource) {
//                if([model.title isEqualToString:@"Date of Birth"]) {
//                    model.content = date;
//                    [weakSelf.tableView reloadData];
//                    break;
//                }
//            }
//            [weakSelf pickViewShow:NO];
//        };
//        [self.view addSubview:_pickView];
//        [_pickView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.width.height.mas_equalTo(self.view);
//            make.top.mas_equalTo(self.view.height);
//            make.bottom.mas_equalTo(-self.view.height);
//        }];
//    }
//    return _pickView;
//}

- (void)configureAlloc {
    [super configureAlloc];
    self.step = @"BASE_INFO";
    self.dataSource = @[].mutableCopy;
    NSArray *titleArr = @[@"First Name",@"Middle Name(Optional)",@"Last Name",
                          @"E-mail address",@"Gender",@"Date of Birth",
                          @"BVN",@"Don’t know your BVN?"];
    NSArray *placeholderArr = @[@"First Name",@"Middle Name",@"Last Name",
                                @"E-mail address",@"",@"",
                                @"Please enter your 11digit BVN",@""];
    NSArray *cellTypeArr = @[@(DCAuthInfoCommonCellTypeRightNun),@(DCAuthInfoCommonCellTypeRightNun),@(DCAuthInfoCommonCellTypeRightNun),
                             @(DCAuthInfoCommonCellTypeRightNun),@(DCAuthInfoCommonCellTypeRightNun),@(DCAuthInfoCommonCellTypeRightDown),
                             @(DCAuthInfoCommonCellTypeRightNun),@(DCAuthInfoCommonCellTypeRightNun)];
    NSArray *canEdit = @[@(YES),@(YES),@(YES),
                         @(YES),@(YES),@(NO),
                         @(YES),@(YES)];
    for (int i = 0; i<titleArr.count; i++) {
        DCAuthInfoUIModel *model = [DCAuthInfoUIModel new];
        model.title = [titleArr objectAtIndex:i];
        model.placeholder = [placeholderArr objectAtIndex:i];
        model.celltype = [[cellTypeArr objectAtIndex:i] intValue];
        model.canEdit = [[canEdit objectAtIndex:i] boolValue];
        [self.dataSource addObject:model];
    }
    
}
- (void)configureUI {
    [super configureUI];
    [super _setCurrntStep:1];
    [self configureTable];
    [self.tableView registerClass:[DCAuthInfoCommonCell class] forCellReuseIdentifier:NSStringFromClass(DCAuthInfoCommonCell.class)];
    [self.tableView registerClass:[DCAuthGenderCell class] forCellReuseIdentifier:NSStringFromClass(DCAuthGenderCell.class)];
    [self.tableView registerClass:[DCUnknownBVNCell class] forCellReuseIdentifier:NSStringFromClass(DCUnknownBVNCell.class)];
    [self.tableView registerClass:[DCAuthFooterView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass(DCAuthFooterView.class)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topRadiusView.mas_bottom).mas_equalTo(-30);
    }];
    [self.view bringSubviewToFront:self.tableView];
    
    mWeakSelf;
    _pickView = [[DCDatePickerView alloc] initWithFrame:CGRectZero];
    _pickView.cancelCallBack = ^(NSString * _Nonnull date) {
        [weakSelf pickViewShow:NO];
    };
    _pickView.sureCallBack = ^(NSString * _Nonnull date) {
        for ( DCAuthInfoUIModel *model in weakSelf.dataSource) {
            if([model.title isEqualToString:@"Date of Birth"]) {
                model.content = date;
                [weakSelf.tableView reloadData];
                break;
            }
        }
        [weakSelf pickViewShow:NO];
    };
    [self.view addSubview:_pickView];
    [_pickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view.height);
        make.bottom.mas_equalTo(-self.view.height);
    }];
}
#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DCAuthInfoUIModel *model = [self.dataSource objectAtIndex:indexPath.row];
    if ([model.title isEqualToString:@"Gender"]) {
        DCAuthGenderCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(DCAuthGenderCell.class)];
        [cell configureDataWithModel:model];
        return cell;
    } else if ([model.title isEqualToString:@"Don’t know your BVN?"]){
        DCUnknownBVNCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(DCUnknownBVNCell.class)];
        [cell configureDataWithModel:model];
        return cell;
    }else {
        DCAuthInfoCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(DCAuthInfoCommonCell.class)];
        cell.indexPath = indexPath;
        cell.delegate = self;
        [cell configureDataWithModel:model];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DCAuthInfoUIModel *model = [self.dataSource objectAtIndex:indexPath.row];
    if ([model.title isEqualToString:@"Date of Birth"]) {
        [self pickViewShow:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    DCAuthInfoUIModel *model = [self.dataSource objectAtIndex:indexPath.row];
    if ([model.title isEqualToString:@"Gender"]) {
        return 95;
    } else if ([model.title isEqualToString:@"Don’t know your BVN?"]){
        return 65;
    }else {
        return 105;        
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    DCAuthFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(DCAuthFooterView.class)];
    BOOL contentNull = [self allInfoContentNull];
    [footer setCommitBtnEnabled:!contentNull];
    footer.delegate = self;
    return footer;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 125;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
#pragma mark --- DCAuthInfoCommonCellDelegate
- (void)authInfoCommonCellEndEditingWithText:(NSString *)text {
    [self.tableView reloadData];
}

- (void)authInfoCommonCellBegainEditingWithText:(NSIndexPath *)index {
    DCAuthInfoUIModel *model = [self.dataSource objectAtIndex:index.row];
    if ([model.title isEqualToString:@"Date of Birth"]) {
        [self pickViewShow:YES];
    }
}
- (BOOL)allInfoContentNull {
    BOOL contentNull= NO;
    for (int i = 0; i < self.dataSource.count-1; i++) {
        DCAuthInfoUIModel *model = self.dataSource[i];
        if(!model.content || !model.content.length) {
            contentNull = YES;
            break;
        }
    }
    return contentNull;
}

#pragma mark --- DCAuthFooterViewDelegate
- (void)authFooterViewNextAction:(id _Nullable )sender {
    /* data: "{
       \"firstName\":\"ghgcg\", // 名
       \"middleName\":\"vvnvv\",//中间名
       \"lastName\":\"gcvjvv\",// 姓
       \"email\":\"gcvhf@gcjg.chvc\",// 电子邮箱
       \"gender\":1, // 性别，男0女1
       \"birth\":\"01-01-1977\",// 出生日期，格式日月年
       \"bvn\":\"12345678900\",// bvn
       }" */
    
    BOOL contentNull = [self allInfoContentNull];
    if(contentNull) {
        return;
    }
    
    NSMutableDictionary *mDic = @{}.mutableCopy;
    DCAuthInfoUIModel *model0 = [self.dataSource objectAtIndex:0];
    [mDic setValue:model0.content forKey:@"firstName"];
    
    DCAuthInfoUIModel *model1 = [self.dataSource objectAtIndex:1];
    [mDic setValue:model1.content forKey:@"middleName"];

    DCAuthInfoUIModel *model2 = [self.dataSource objectAtIndex:2];
    [mDic setValue:model2.content forKey:@"lastName"];

    
    DCAuthInfoUIModel *model3 = [self.dataSource objectAtIndex:3];
    BOOL isEmail = [DCProjectTool isValidateEmail:model3.content];
    if(!isEmail) {
        return;
    }
    [mDic setValue:model3.content forKey:@"email"];

    
    DCAuthInfoUIModel *model4 = [self.dataSource objectAtIndex:4];
    [mDic setValue:model4.content forKey:@"gender"];

    
    DCAuthInfoUIModel *model5 = [self.dataSource objectAtIndex:5];
    [mDic setValue:model5.content forKey:@"birth"];

    
    DCAuthInfoUIModel *model6 = [self.dataSource objectAtIndex:6];
//    BOOL isBVN = [DCProjectTool isBVNText:model6.content];
//    if(!isBVN) {
//        return;
//    }
    [mDic setValue:model6.content forKey:@"bvn"];
    
    NSString *jsonStr = mDic.mj_JSONString;
    [self saveInfo2:jsonStr];

}

- (void)authFooterViewAgreementAction:(id _Nullable )sender {
    [self pushPrivacyAgreement];
}

- (void)pickViewShow:(BOOL)show {
    [self.pickView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(show?0:self.view.height);
        make.bottom.mas_equalTo(show?0:-self.view.height);
    }];
    self.pickView.backgroundColor = [[UIColor colorWithString:@"#000000"] colorWithAlphaComponent:0];

    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        self.pickView.backgroundColor = [[UIColor colorWithString:@"#000000"] colorWithAlphaComponent:0.7];
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
